package com.atguigu.interceptor;

import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.util.WebUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取到用户信息
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        //判断用户信息是否存在(也就是是否已经登录)
        //根据是否登录返回不同数据
        if(userInfo == null){
            //用户未登录,返回到前端
            WebUtil.writeJSON(response, Result.build(null, ResultCodeEnum.LOGIN_AUTH));
            return false;
        }
        //用户登录了,进入handler
        return true;
    }
}
